﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;
using MyGeneration.dOOdads;

namespace WebUI.EditPages
{
    public partial class EditSelectedProblem : BasePage
    {
        int id;

        public String SelectedProblemsKey
        {
            get
            {
                if (ViewState["SelectedProblemsKey"] == null)
                    ViewState["SelectedProblemsKey"] = "SelectedProblemsKey";
                return ViewState["SelectedProblemsKey"].ToString();
            }
        }

        public RepairJobProblems SelectedProblems
        {
            get
            {
                return Session[SelectedProblemsKey] as RepairJobProblems;
            }
            set
            {
                Session[SelectedProblemsKey] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);

            if (Request.QueryString[ID] != null && Request.QueryString[ID] != "")
                id = int.Parse(Request.QueryString[ID]);
            else
            {
                CloseDlg(0);
                return;
            }

            if (!IsPostBack)
            {
                string oldFilter = SelectedProblems.Filter;
                SelectedProblems.Filter = RepairJobProblems.ColumnNames.RepairModelProblemID + "=" + id.ToString();
                if (SelectedProblems.RowCount > 0)
                {
                    txtDesc.Text = SelectedProblems.s_Description;
                    txtPrice.Text = SelectedProblems.s_Price;
                    SelectedProblems.Filter = oldFilter;
                }
                else
                {
                    SelectedProblems.Filter = oldFilter;
                    CloseDlg(0);
                    return;
                }
            }
        }

        public void btnSave_OnClick(Object sender, EventArgs e)
        {
            if(Save())
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("window.returnValue={0};", 1), true);
        }

        private bool Save()
        {
            string oldFilter = SelectedProblems.Filter;
            SelectedProblems.Filter = RepairJobProblems.ColumnNames.RepairModelProblemID + "=" + id.ToString();

            if (SelectedProblems.RowCount > 0)
            {
                SelectedProblems.s_Description = txtDesc.Text;
                SelectedProblems.s_Price = txtPrice.Text;
                SelectedProblems.Filter = oldFilter;
                return true;
            }

            SelectedProblems.Filter = oldFilter;
            return false;
        }

        public void btnSaveAndClose_OnClick(Object sender, EventArgs e)
        {
            if(Save())
                CloseDlg(1);
        }
    }
}